<template>
  <view>
    <!-- 搜索框-->
    <view class="search">
      <my-search @click='toSearch()'></my-search>
    </view>
    
    <!-- 轮播图-->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpackages/goods_detail/goods_detail?goods_id='+ item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 分类导航-->
    <view class="nav">
      <view v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    
    <!-- 楼层-->
    <view class="floor">
      <view class="floorTitle" v-for="(item,index) in FloorList" :key="index">
        <!-- 楼层标题-->
        <image :src="item.floor_title.image_src"></image>
        <!--楼层图片 -->
        <view class="floorImg">
          <!-- 楼层左侧大图-->
          <navigator class="floorBig" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" mode="widthFix"
              :style="{width: item.product_list[0].image_width + 'rpx'}"></image>
          </navigator>
          <!-- 楼层右侧小图-->
          <view class="floorSmall">
            <navigator v-for="(item2,index) in item.product_list" :key="index" v-if="index !== 0" :url="item2.url">
              <image :src="item2.image_src" mode="widthFix"
              :style="{width: item2.image_width - 15 + 'rpx'}"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
import badgeMix  from '@/mixins/tabbar-badge.js'

  export default {
    mixins:[badgeMix ],
    data() {
      return {
        swiperList: [], //轮播图数据
        navList: [], //分类导航数据
        FloorList: [] //楼层数据
      };
    },
    onLoad() {
      this.getSwiperList() //轮播图
      this.getNavList() //分类导航
      this.getFloorList() //楼层数据
    },
    methods: {
      //获取轮播图
      async getSwiperList() {
        //1.发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        //2.请求失败后，调用封装好的方法
        if (res.meta.status !== 200) return uni.$showMsg()
        //3.请求成功后
        this.swiperList = res.message
      },

      //获取分类导航
      async getNavList() {
        //1.发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        //2.请求失败后，调用封装好的方法
        if (res.meta.status !== 200) return uni.$$showMsg()
        //3.请求成功后
        this.navList = res.message
      },

      //分类导航分类的跳转处理
      navClickHandler(item) {
        if (item.name == "分类") {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },

      //楼层标题数据
      async getFloorList() {
        //1.发请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        //2.请求失败后，调用封装好的方法
        if (res.meta.status !== 200) return uni.$$showMsg()
        //数据处理
       res.message.forEach((floor) => {
         floor.product_list.forEach((floorUrl)=>{
           //手动给这个product_list(经过循环后的floorUrl就是一个一个product_list)加上一个url属性并且参数为我们分隔出来的
           floorUrl.url = '/subpackages/goods_list/goods_list?' + floorUrl.navigator_url.split('?')[1]
         })
       })
        //3.请求成功后
        this.FloorList = res.message
      },
      
      //前往搜索页面
      toSearch() {
        uni.navigateTo({
          url:'/subpackages/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;
    .swiper-item image {
      width: 100%;
    }
  }

  .nav {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor {
    padding-left: 10px;
    .floorTitle {
      margin-top: 15px;
      image {
        display: flex;
        width: 100%;
        height: 60rpx;
      }
      .floorImg {
        display: flex;
        .floorBig{
          
        }
        .floorSmall{
         display: flex;
         flex-wrap: wrap;
         justify-content: space-around;
        }
      }
    }
  }
  
  .search {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
